package com.itheima.controller.backend;

import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author Encounter
 * @date 2024/10/10 15:08<br/>
 */
@RestController
@RequestMapping("/ordersetting")
@Slf4j
public class OrderSettingController
    {
        @Autowired
        private OrderSettingService orderSettingService;
        
        /**
         * 导出Excel
         *
         * @param filename 文件名
         * @param response 响应
         */
        @GetMapping("/download")
        public void download(String filename, HttpServletResponse response)
            {
                try
                    {
                        log.info("开始导出预约设置信息");
                        orderSettingService.download(filename, response);
                    }
                catch (Exception e)
                    {
                        throw new RuntimeException(e);
                    }
            }
        
        /**
         * 文件上传
         *
         * @param excelFile Excel 文件
         * @return {@link Result }
         */
        @PostMapping("/upload")
        public Result upload(MultipartFile excelFile)
            {
                try
                    {
                        log.info("要上传的文件名：{}", excelFile.getOriginalFilename());
                        orderSettingService.upload(excelFile);
                        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
                    }
                catch (Exception e)
                    {
                        //throw new RuntimeException(e);
                        return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
                    }
            }
        
        /**
         * 按订单日期编辑编号
         *
         * @param orderSetting 订单设置
         * @return {@link Result }
         */
        @PutMapping("/editNumberByOrderDate")
        public Result editNumberByOrderDate(@RequestBody OrderSetting orderSetting)
            {
                try
                    {
                        log.info("要修改的信息：{}", orderSetting);
                        int count = orderSettingService.update(orderSetting);
                        if (count > 0)
                            {
                                return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
                            }
                        else
                            {
                                return new Result(false, MessageConstant.ORDERSETTING_FAIL);
                            }
                    }
                catch (Exception e)
                    {
                        //throw new RuntimeException(e);
                        return new Result(false, MessageConstant.ORDERSETTING_FAIL);
                    }
            }
        
        /**
         * 按月获取订单设置
         *
         * @param month 月
         * @return {@link Result }
         */
        @GetMapping("/getOrderSettingByMonth")
        public Result getOrderSettingByMonth(String month)
            {
                try
                    {
                        log.info("要查询的月份：{}", month);
                        List<OrderSetting> orderSettingList = orderSettingService.getOrderSettingByMonth(month);
                        if (orderSettingList != null && !orderSettingList.isEmpty())
                            {
                                return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, orderSettingList);
                            }
                        else
                            {
                                return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
                            }
                    }
                catch (Exception e)
                    {
                        //throw new RuntimeException(e);
                        return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
                    }
            }
    }
